package com;

import com.alibaba.druid.pool.DruidDataSource;
import com.orm_plus.SimpleSqlSessionFactory;
import com.spring.annotation.*;
import com.orm_plus.MapperScannerConfig;

import javax.sql.DataSource;

@Configuration
@ComponentScan("com.peanut")
@EnableAop
@EnableTranscation
public class Config {
	/**
	 * 数据源连接池
	 * @return
	 */
	@Bean(destroyMethod = "close")
	public DataSource dataSource() {
		DruidDataSource ds = new DruidDataSource();
		ds.setUrl("jdbc:mysql://localhost:3306/web?serverTimezone=UTC%2B8");
		ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
		ds.setUsername("root");
		ds.setPassword("20080808cs");
		return ds;
	}
	
	/**
	 * sqlSession 工厂
	 * @param ds
	 * @return
	 */
	@Bean
	public SimpleSqlSessionFactory sqlSessionFactory(DataSource ds){
		SimpleSqlSessionFactory simpleSqlSessionFactory = new SimpleSqlSessionFactory("mybatis-config.xml");
		simpleSqlSessionFactory.setDataSource(ds);
		return simpleSqlSessionFactory;
	}
	
	/**
	 * mapper接口扫描配置
	 * @param ds
	 * @return
	 */
	@Bean
	public MapperScannerConfig mapperScannerConfig(DataSource ds) {
		MapperScannerConfig mapperScannerConfig = new MapperScannerConfig();
		mapperScannerConfig.setDataSource(ds);
		mapperScannerConfig.setBasePackage("com.peanut.mapper");
		return mapperScannerConfig;
	}
}
